﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Tencent.OA.App
{
    /// <summary>
    /// 资源管理器
    /// </summary>
    public class ResourceManager
    {
        #region -- Private --

        private static IList<Country> _countrys;
        private static IList<City> _citys;
        private static string _resourceFile;
        private static Dictionary<string, string> _messDict;
        #endregion

        #region -- Public --

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="resourceFile">资源文件路径</param>
        //public ResourceManager(string resourceFile)
        //{
        //    if (File.Exists(resourceFile))
        //    {
        //        _resourceFile = resourceFile;
        //        _countrys = null;
        //        _citys = null;
        //    }
        //    else
        //    {
        //        throw new IOException("文件不存在 :" + resourceFile);
        //    }
        //}
        /// <summary>
        /// 默认构造器
        /// </summary>
        static ResourceManager()
        {
            string resourceFile = Path.Combine(AssemblyInfo.GetAssemblyPath(), "Resources.xml");
            if (File.Exists(resourceFile))
            {
                _resourceFile = resourceFile;
                _countrys = null;
                _citys = null;
            }
            else
            {
                throw new IOException("资源文件不存在 :" + resourceFile);
            }
        }
        /// <summary>
        /// 资源文件路径
        /// </summary>
        public static string ResourceFile
        {
            get { return ResourceManager._resourceFile; }
            set { ResourceManager._resourceFile = value; }
        }

        /// <summary>
        /// 重新载入资源数据，如果修改了资源文件，请调用该方法刷新数据。
        /// </summary>
        public static void ReloadResource()
        {
            _countrys = null;
            _citys = null;
        }

        /// <summary>
        /// 读取所有的国家信息。
        /// </summary>
        /// <returns>国家列表。</returns>
        public static IList<Country> GetAllCountry()
        {
            if (_countrys == null)
            {
                XElement doc = XElement.Load(_resourceFile);
                var query = from o in doc.Descendants("countrys").Descendants("country")
                            select new Country
                            {
                                Id = o.Attribute("id").Value,
                                Name = o.Attribute("name").Value
                            };
                _countrys = query.ToList();
            }
            return _countrys;
        }
        /// <summary>
        /// 读取所有县-市-市信息。
        /// </summary>
        /// <returns>城市列表</returns>
        public static IList<City> GetAllCity()
        {
            if (_citys == null)
            {
                XElement doc = XElement.Load(_resourceFile);
                var query = from o in doc.Descendants("citys").Descendants("city")
                            select new City
                            {
                                Id = Int32.Parse(o.Attribute("id").Value),
                                level = Int32.Parse(o.Attribute("level").Value),
                                ParentId = Int32.Parse(o.Attribute("parentId").Value),
                                Name = o.Attribute("name").Value
                            };
                _citys = query.ToList();
            }
            return _citys;
        }
        /// <summary>
        /// 取指定ID、指定level的城市。
        /// </summary>
        /// <param name="level">级别。0:省-直辖市-港澳台地区  1:一级城市 2:二级城市 3：三级城市</param>
        /// <param name="id">要选择的城市的所属区域ID ,如为null，则不筛选id</param>
        /// <returns></returns>
        public static IList<City> GetChildCitys(int level, int? id)
        {
            if (_citys == null)
                GetAllCity();

            if (id == null)
            {
                return _citys.Where(o => o.level == level).ToList();
            }
            else
            {
                return _citys.Where(o => (o.level == level) && (o.ParentId == id.Value)).ToList();
            }
        }
        /// <summary>
        /// 取所有信息的字典
        /// </summary>
        /// <returns></returns>
        public static  Dictionary<string, string> MessageDict()
        {
            XElement doc = XElement.Load(_resourceFile);
            var query = from o in doc.Descendants("messageString").Descendants("item")
                        select new MsgString
                        {
                            Id = o.Attribute("id").Value,
                            Text = o.Attribute("value").Value,                          
                        };
            IList<MsgString> mess = query.ToList();
            _messDict = new Dictionary<string, string>();
            foreach (MsgString s in mess)
            {
                if (_messDict.ContainsKey(s.Id))
                {
                    throw new Exception("MessageString存在重复的ID:" + s.Id);
                }
                else
                {
                    _messDict.Add(s.Id, s.Text);
                }
            }
            return _messDict;
        }

        /// <summary>
        /// 访问配置文件中的字符串常量。
        /// </summary>
        public static Dictionary<string, string> MessageString
        {
            get
            {
                if (_messDict == null)
                    MessageDict();
                return _messDict;
            }
        }
        #endregion
    }
}
